package core.playerManager.swfManager
{
    import com.hydrotik.queueloader.QueueLoader;
    import com.hydrotik.queueloader.QueueLoaderEvent;
    
    import core.playerManager.swfManager.AddvancePlayerEvent;
    
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    
    import memory.*;
    
    import mx.core.*;
    import mx.messaging.channels.*;
    /**
    * 需要实现的功能，用户只要传入存储了SWF的相关信息的数组：array={url:--,x:--,y:--,scaleX:--,scaleY:--};
    * 
    */ 
    public class AddvanceSwfPlayer extends Sprite
    {
        private var swfUrls:Array;            //存储SWF信息的数组
        private var swfs:Array;               //存储所有加载完毕的SWF
        private var loader:QueueLoader;       //loader
        private var numSwf:int=-1;            //所要显示的SWF的总数
        public function AddvanceSwfPlayer(swfUrls:Array)
        {
            swfs = new Array();
            this.swfUrls =swfUrls ;
            this.numSwf=swfUrls.length;
            return;
        }// end function
        /**
        * 初始化
        */ 
        private function init():void
        {
        	loader=new QueueLoader(false,new LoaderContext(false,ApplicationDomain.currentDomain));
        	for each(var obj:Object in this.swfUrls)
        	{
        		loader.addItem(obj.url);
        	}
        	loader.addEventListener(QueueLoaderEvent.ITEM_COMPLETE,on_Item_complete);
        }
        /**
        * 单个Swf加载完毕
        */ 
        private function on_Item_complete(e:QueueLoaderEvent):void
        {
        	if(swfUrls.length==numSwf) this.dispatchEvent(new AddvancePlayerEvent(AddvancePlayerEvent.READY));//抛出Ready事件
    		var swfInfo:Object=swfUrls.shift();
    		var movieClip:MovieClip=MovieClip(loader.getItemByTitle(swfInfo.url));
    		movieClip.x=swfInfo.x;movieClip.y=swfInfo.y;movieClip.scaleX=swfInfo.scaleX;movieClip.scaleY=swfInfo.scaleY;
    		this.addChild(movieClip);
    		if(swfUrls.length==0)
			{
				this.dispatchEvent(new AddvancePlayerEvent(AddvancePlayerEvent.COMPLETE)); //抛出全部加载完毕的事件
			}
        }
        /**
        * 开始加载SWF
        */ 
        public function start() : void
        {
        	loader.execute();
        }// end function
        /**
        * 继续播放SWF
        */ 
        public function resume() : void
        {
            var mc:MovieClip;
            for each (mc in swfs)
            {
            	 MovieClip(mc).play();
            }
            return;
        }// end function
        /**
        * 暂停所有的SWF 
        */ 
        public function pause() : void
        {
            var mc:MovieClip;
            for each (mc in swfs)
            {
            	MovieClip(mc).stop();
            }
            return;
        }// end function
        /**
        * 回收内存
        */ 
        public function dispose() : void
        {
        	swfUrls=null;
        	if(loader!=null)
        	{
        		MemoryRecovery.getInstance().gcFun(loader,QueueLoaderEvent.ITEM_COMPLETE,on_Item_complete);
        		loader.dispose();
        		loader=null;
        	}
            for each (var mc:MovieClip in swfs)
            {
            	if(mc!=null)
            	{
            		if(mc.parent!=null)
            		{
            			mc.parent.removeChild(mc);
            		}
            		if(mc.hasOwnProperty("dispose"))
            		{
            			mc["dispose"]();
            		}
            		mc=null;
            	}
            }
            swfs=null;
            MyGC.gc();
            return;
        }// end function

    }
}
